package com.us.usws.site.ws.entity.shared;

import java.util.ArrayList;
import java.util.List;

import com.us.usws.model.domain.Meeting;
import com.us.usws.site.ws.entity.WSEntity;
import com.us.usws.site.ws.entity.WSEntityFactory;
import com.us.usws.site.ws.entity.shared.meeting.WSSharedMeetingDetail;
import com.us.usws.site.ws.entity.shared.meeting.WSSharedMeetings;
import com.us.usws.support.uitls.SimpleDataPageUtil;

public class WSSharedEntityPackGenerator {

	//private static Log log = LogFactory.getFactory().getInstance(WSEntityPackGenerator.class);

	private static WSSharedEntityPackGenerator instance = new WSSharedEntityPackGenerator();

	private WSSharedEntityPackGenerator() {
	}

	public static WSSharedEntityPackGenerator getInstance() {
		return instance;
	}

	/*---------------- public methods -----------------*/

	/**
	 * get meetings
	 * @param channelUid
	 * @param results
	 * @param pu
	 * @return
	 */
	public WSEntity getMeetingsPack(List<Meeting> meetings, SimpleDataPageUtil pu, PageUrlCallback callback) {
		if (meetings == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}

		//prepare return videos
		WSSharedMeetings data = new WSSharedMeetings(pu.getPageSize(), pu.getPageNo(), pu.getRowCount(), pu.getPageSize());
		//data.setPrevPageUrl(WSResourceURIReplacer.getChannelVideosListURLWithPageNo(channelUid, pu.getPageNo() - 1, pu.getPageCount()));
		//data.setNextPageUrl(WSResourceURIReplacer.getChannelVideosListURLWithPageNo(channelUid, pu.getPageNo() + 1, pu.getPageCount()));
		data.setPrevPageUrl(callback.getPrevPageUrl());
		data.setNextPageUrl(callback.getNextPageUrl());

		List<WSSharedMeetingDetail> records = new ArrayList<WSSharedMeetingDetail>(meetings.size());
		//loop for channels
		for (Meeting eachM : meetings) {
			//convert to WS entity
			WSSharedMeetingDetail vd = PackEntityConverter.genWSSharedMeeting(eachM);

			//add one channel to data
			records.add(vd);
		}

		data.setRecords(records);

		//pack records
		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}

	/*
	 * --------------------- inner class -----------------------
	 */
	public static interface PageUrlCallback {
		public String getPrevPageUrl();

		public String getNextPageUrl();
	}

	public WSEntity getMeetingPack(Meeting m) {
		if (m == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}

		//prepare return videos
		WSSharedMeetingDetail data = PackEntityConverter.genWSSharedMeeting(m);

		//pack records
		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}
}
